package com.zhangtao.demo.common.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class IOUtil {

	public static void transferToTargetFile(InputStream in, File target) {
		int length = 1024;
		if (in != null) {
			try {
				byte[] b = new byte[length];
				FileOutputStream fos = new FileOutputStream(target);
				while (in.read(b) > 0) {
					fos.write(b);
				}
				fos.flush();
				fos.close();
				in.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws IOException {
		Document doc = Jsoup.connect("https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=2&tn=baiduhome_pg&wd=java%20jsoup&rsv_spt=1&oq=java%2520soup&rsv_pq=907b713b0019c65c&rsv_t=e7eeZwFAIZQqLw2oPl8K%2FCH7GdONL%2FRUV%2F6abXTQS9aEkc%2BNpSHxHMcaU%2Fr7fBjVZyB0&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_btype=t&inputT=818&rsv_sug3=18&rsv_n=2&rsv_sug1=17&rsv_sug7=100&rsv_sug2=0&rsv_sug4=997").get();
		System.out.println(doc.title());
		Elements newsHeadlines = doc.select("#mp-itn b a");
		for (Element headline : newsHeadlines) {
			System.out.println(headline.attr("title") + "   ;;  " + headline.absUrl("href"));
		}
	}
}
